<?php
class EPMenuList extends CComponent{
	const POS_BEGIN=0;
	const POS_LEADING=1;
	const POS_MAIN=2;
	const POS_ADDITION=3;
	const POS_END=4;

	private $_menuItems=array();
	private $_parentItem=NULL;
	private $_menuCss=array();
	

	public $menuId;
	public $menuDepth=1;
	public $menuHtmlOptions=array();
	public $cssPrefix='';
	public $visible=true;

	function __construct(&$parent=NULL){
		if($parent!==NULL){
			$this->_parentItem=&$parent;
			$this->cssPrefix=$parent->cssPrefix;
		}
	}



	public function getMenuItems(){
		$menuItems=array();
		if(!empty($this->_menuItems[EPMenuList::POS_BEGIN])){
			foreach($this->_menuItems[EPMenuList::POS_BEGIN] as $m){
				$menuItems[]=$m;
			}
		}
		if(!empty($this->_menuItems[EPMenuList::POS_LEADING])){
			foreach($this->_menuItems[EPMenuList::POS_LEADING] as $m){
				$menuItems[]=$m;
			}
		}
		if(!empty($this->_menuItems[EPMenuList::POS_MAIN])){
			foreach($this->_menuItems[EPMenuList::POS_MAIN] as $m){
				$menuItems[]=$m;
			}
		}
		if(!empty($this->_menuItems[EPMenuList::POS_ADDITION])){
			foreach($this->_menuItems[EPMenuList::POS_ADDITION] as $m){
				$menuItems[]=$m;
			}
		}
		if(!empty($this->_menuItems[EPMenuList::POS_END])){
			foreach($this->_menuItems[EPMenuList::POS_END] as $m){
				$menuItems[]=$m;
			}
		}
		return $menuItems;
	}

	public function getSubMenuById($subId){
		$menus=$this->getMenuItems();
		foreach($menus as $menu){
			if($menu->hasSubMenu()){
				$submenu=$menu->getSubMenu();
				if($submenu->menuId==$subId){
					return $submenu;
				}
			}
		}
		return NULL;
	}

	public function addMenuItem($menuItem,$pos=EPMenuList::POS_MAIN){
		if(!isset($this->_menuItems[$pos])){
			$this->_menuItems[$pos]=array();
		}
		$menuItem->menuDepth=$this->menuDepth;
		$this->_menuItems[$pos][]=$menuItem;
	}


	function createMenuItem($pos=EPMenuList::POS_MAIN){
		$menuItem=new EPMenuItem($this);
		$this->addMenuItem($menuItem,$pos);
		return $menuItem;
	}

	function addMenuCss($css){
		$css=$this->cssPrefix.$css;
		if(!empty($css)&&!in_array($css,$this->_menuCss)){
			$this->_menuCss[]=$css;
		}
	}

	function getMenuCss(){
		return $this->_menuCss;
	}

	function getParent(){
		return $this->_parentItem;
	}


}